package com.daxt.controller.filemanager;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.archives.ArchivesDocMapper;
import com.daxt.mapper.archives.ArchivesTestingStandardMapper;
import com.daxt.mapper.sys.ArchivesTypeMapper;
import com.daxt.model.service.archives.vo.ArchivesDoc;
import com.daxt.model.service.archives.vo.ArchivesTestingStandard;
import com.daxt.model.service.archives.vo.ArchivesType;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "检测配置")
@RestController
@RequestMapping("/testing")
public class TestingManageController {
	
	@Autowired
	private ArchivesTestingStandardMapper archivesTestingStandardMapper;
	@Autowired
	private ArchivesDocMapper archivesDocMapper;
	@Autowired
	private ArchivesTypeMapper archivesTypeMapper;
	
	@ApiOperation(value = "规范性检测列表", notes = "")
	@GetMapping("/manage/standard/{typeId}")
	public Result<List<ArchivesTestingStandard>> standardList(@PathVariable("typeId")String typeId) {
		QueryWrapper<ArchivesTestingStandard> queryWrapper=new QueryWrapper<ArchivesTestingStandard>();
		queryWrapper.eq("typeId", typeId);
		List<ArchivesTestingStandard> list = archivesTestingStandardMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),"", list);
	}
	@ApiOperation(value = "规范性检测添加", notes = "")
	@PostMapping("/manage/standard")
	public Result<String> standardSave(@RequestBody ArchivesTestingStandard archivesTestingStandard) {
	    String cronValue = archivesTestingStandard.getCronValue();
	    //String dataColumn = archivesTestingStandard.getDataColumn();
	    String typeId = archivesTestingStandard.getTypeId();
	    ArchivesType typeEntity = archivesTypeMapper.selectById(typeId);
	  
	    if(typeEntity==null)
	    	return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "门类查找不到！！！", null);
	    if("1".equals(archivesTestingStandard.getCtype())){
	    	//List<String> docIds = archivesTestingStandardMapper.selectFile();
	    }else if("2".equals(archivesTestingStandard.getCtype())){
	    	
	    }else if("3".equals(archivesTestingStandard.getCtype())){
	    	
	    }else if("4".equals(archivesTestingStandard.getCtype())){
	    	try {
	    	QueryWrapper<ArchivesDoc> queryWrapperValue=new QueryWrapper<>();
	    	String cronc="";
	    	if(cronValue.contains("aconcata")) {
	    		String[] split = cronValue.split("aconcata");
	    		for (String string : split) {
					if(StringUtils.isEmpty(cronc)) {
						cronc=string;
					}else {
						cronc+=" and "+string;
					}
				}
	    	}else {
	    		cronc=cronValue;
	    	}
	    	queryWrapperValue.last("where "+cronc);
	    	queryWrapperValue.select("id");
			archivesDocMapper.selectList(queryWrapperValue);
	    	}catch (Exception e) {
				e.printStackTrace();
				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "参数异常！！！", null);
			}
	    }else {
	    	return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类型异常！！！", null);
	    }
	   
		//archivesDocMapper.selectList(queryWrapperCronValue);
	    archivesTestingStandardMapper.insert(archivesTestingStandard);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),"", "");
	}
	
	@ApiOperation(value = "规范性检测修改", notes = "")
	@PutMapping("/manage/standard")
	public Result<String> standardEdit(@RequestBody ArchivesTestingStandard archivesTestingStandard) {
		 String cronValue = archivesTestingStandard.getCronValue();
		    //String dataColumn = archivesTestingStandard.getDataColumn();
		    String typeId = archivesTestingStandard.getTypeId();
		    ArchivesType typeEntity = archivesTypeMapper.selectById(typeId);
		  
		    if(typeEntity==null)
		    	return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "门类查找不到！！！", null);
		    if("1".equals(archivesTestingStandard.getCtype())){
		    	archivesTestingStandard.setFileType("doc");
		    }else if("2".equals(archivesTestingStandard.getCtype())){
		    	
		    }else if("3".equals(archivesTestingStandard.getCtype())){
		    	
		    }else if("4".equals(archivesTestingStandard.getCtype())){
		    	try {
		    	QueryWrapper<ArchivesDoc> queryWrapperValue=new QueryWrapper<>();
		    	String cronc="";
		    	if(cronValue.contains("aconcata")) {
		    		String[] split = cronValue.split("aconcata");
		    		for (String string : split) {
						if(StringUtils.isEmpty(cronc)) {
							cronc=string;
						}else {
							cronc+=" and "+string;
						}
					}
		    	}else {
		    		cronc=cronValue;
		    	}
		    	queryWrapperValue.last("where "+cronc);
		    	queryWrapperValue.select("id");
				archivesDocMapper.selectList(queryWrapperValue);
		    	}catch (Exception e) {
					e.printStackTrace();
					return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "参数异常！！！", null);
				}
		    }else {
		    	return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类型异常！！！", null);
		    }
		   
			//archivesDocMapper.selectList(queryWrapperCronValue);
		    archivesTestingStandardMapper.updateById(archivesTestingStandard);
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),"", "");
	}
	
	@ApiOperation(value = "规范性检测删除", notes = "")
	@DeleteMapping("/manage/standard")
	public Result<String> standardSave(@RequestBody List<String> ids) {
	    archivesTestingStandardMapper.deleteBatchIds(ids);
	    return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),"", "");
	}
	
}
